package com.mojo.linearlist;

import java.util.Scanner;

/**
 * 线性表-队列,数组实现
 * 1.front-指向队列头部元素
 * 2.rear-指向队列尾部元素
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/8/30 21:47
 */
public class ArrayQueue {
    private int front;
    private int rear;
    private int maxSize;

    int[] arr;

    /**
     * 初始化队列
     */
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        //尾部下标数值等于数组最大索引值
        return rear == maxSize - 1;
    }

    public void addQueue(int n) {
        //判断队列是否已满
        if (isFull()) {
            throw new RuntimeException("队列已满~");
        }
        rear++;
        //移动下标
        arr[rear] = n;
    }

    public int getElement() {
        //判断是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列无元素~");
        }
        front++;
        return arr[front];
    }

    public void show() {

    }

    /**
     * 查询队列头元素
     */
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("队列无元素~");
        }
        return arr[front];
    }

    public static void main(String[] str) {
        ArrayQueue aq = new ArrayQueue(5);
        aq.addQueue(1);

        Scanner scanner = new Scanner(System.in);

        boolean run = true;
        while (run) {
            System.out.println("s(show),打印队列");
            System.out.println("g(get),取出队列数据");
            System.out.println("p(peek),查看队列头数据");
            System.out.println("a(add),向队列添加数据");
            char c = scanner.next().charAt(0);
            switch (c) {
                case 'a':
                    System.out.println("请输入要添加的数字");

                    try {
                        aq.addQueue(scanner.nextInt());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    System.out.println("获取队列元素:");

                    try {
                        System.out.println(aq.getElement());
                    } catch (Exception e) {
                        System.out.println(">>>" + e.getMessage());
                    }
                    break;
                case 'p':
                    System.out.println("查看队列元素:");
                    try {
                        System.out.println(aq.peek());
                    } catch (Exception e) {
                        System.out.println(">>>" + e.getMessage());
                    }
                    break;
                case 's':
                    System.out.println("打印队列元素:");
                    for (int i : aq.arr) {
                        System.out.printf("%d\t", i);
                    }
                    System.out.println();
                    break;
                case 'e':
                    System.out.println("退出:");
                    run = false;
                    scanner.close();
                    break;
                default:
                    break;

            }
        }
    }
}
